﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Utils
{
    /// <summary>
    /// MD5摘要
    /// </summary>
    public class Md5Helper
    {
        #region MD5 字符串 
        /// <summary>
        /// 字符串 MD5 比较
        /// </summary>
        /// <param name="text">文本</param>
        /// <param name="hash">MD5 字符串</param>
        /// <returns>bool</returns>
        public static bool MdtCompare(string text, string hash)
        {
            var newvalue = Md5(text);
            return newvalue.ToLower().Equals(hash.ToLower());

        }


        /// <summary>  
        /// 128位MD5算法加密字符串  
        /// </summary>  
        /// <param name="text">要加密的字符串</param>      
        public static string Md5(string text)
        {

            ////32位大写
            //using var md5 = MD5.Create();
            //var result = md5.ComputeHash(Encoding.UTF8.GetBytes(text));
            //var strResult = BitConverter.ToString(result);
            //string result3 = strResult.Replace("-", "");
            //Console.WriteLine(result3);

            //32位大写
            using var md5 = MD5.Create();
            var data = md5.ComputeHash(Encoding.UTF8.GetBytes(text));
            StringBuilder builder = new StringBuilder();
            // 循环遍历哈希数据的每一个字节并格式化为十六进制字符串 
            for (int i = 0; i < data.Length; i++)
            {
                builder.Append(data[i].ToString("X2"));
            }
            string result4 = builder.ToString();
            return result4;
        }

        /// <summary>  
        /// 128位MD5算法加密Byte数组  
        /// </summary>  
        /// <param name="data">要加密的Byte数组</param>      
        public static string Md5(byte[] data)
        {
            if (data == null || data.Length == 0)
            {
                return "";
            }
            try
            {
                //创建MD5密码服务提供程序  
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

                //计算传入的字节数组的哈希值  
                byte[] outBytes = md5.ComputeHash(data);

                //释放资源  
                md5.Clear();

                ////返回MD5值的字符串表示  
                //return Convert.ToBase64String(result);
                string outString = "";
                foreach (byte t in outBytes)
                {
                    outString += t.ToString("x2");
                }
                return outString.ToUpper();
            }
            catch (Exception ex)
            {
                throw new Exception("MD5() fail, error:" + ex.Message);
            }
        }
        #endregion

        #region Md5 文件
        /// <summary>
        /// 获取文件的MD5
        /// </summary>
        /// <param name="filepath">文件路径</param>
        /// <returns></returns>
        public static string Md5ToFile(string filepath)
        {
            try
            {
                FileStream file = new FileStream(filepath, FileMode.Open);
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(file);
                file.Close();

                StringBuilder sb = new StringBuilder();
                foreach (byte t in retVal)
                {
                    sb.Append(t.ToString("x2"));
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("MD5ToFile() fail, error:" + ex.Message);
            }
        }
        #endregion
    }
}
